<?php

namespace app\admin\controller;

use app\index\controller\Common;

/**
 * 权限管理-规则控制器
 * Class Rules
 * @package app\admin\controller
 */
class Rules extends Common
{
    private  $db;
    public function __construct()
    {
        parent::__construct();
        $this->db =new \app\common\model\Rules();
    }
    public  function index(){
        $arrTreeData = $this->db->getArrTreeData();
        $this->assign('arrTreeData',$arrTreeData);
        return view();
    }
    public function add(){
        if(request()->isPost()){
            $res = $this->db->add(input('post.'));
            if($res){
                $this->success('添加成功','admin/rules/index');
            }else{
                $this->error($res['msg']);
            }
        }
        return view();
    }

    //添加子规则
    public function addSon(){
        $id = input('param.id');
        //处理所属规则
        $ruleData = db('authrule')->field(['id','title','nav'])->find($id);
        $this->assign('ruleData',$ruleData);
        if(request()->isPost()){
            $res = $this->db->add(input('post.'));
            if($res){
                $this->success('添加成功','admin/rules/index');
            }else{
                $this->error($res['msg']);
            }
        }
        return view();
    }

    //编辑
    public function edit(){
        $id = input('param.id');
        //处理旧数据
        $oldData = db('authrule')->find($id);
        $this->assign('oldData',$oldData);

        //获取所属规则  把自己和自己的子规则去掉
        $ruleData = $this->db->getRule($id);
        $this->assign('ruleData',$ruleData);

        if(request()->isPost()){
            $res = $this->db->edit(input('post.'));
            if($res){
                $this->success($res['msg'],'admin/rules/index');
            }else{
                $this->error($res['msg']);
            }
        }
        return view();
    }

    //删除
    public function del(){
        $id = input('param.id');
        $res = $this->db->del($id);
        if($res){
            $this->success($res['msg'],'admin/rules/index');
        }else{
            $this->error($res['msg']);
        }
    }
}
